<?php
    /**
     * Created by JetBrains PhpStorm.
     * User: xiahuiqin
     * Date: 13-3-25
     * Time: 上午10:37
     * To change this template use File | Settings | File Templates.
     */
    class Index extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
        }

        /**
         * 前台会员登录
         */
        function login()
        {

        }

        /**
         * 显示验证码图片
         *
         */
        function show()
        {
            $this->authcode->show();
        }

        /**
         * 前台会员注册
         */
        function register()
        {
            $datas = array();

            $this->form_validation->set_rules('name', '用户名', 'trim|required|max_length[15]');
            $this->form_validation->set_rules('password', '密码', 'trim|required');
            $this->form_validation->set_rules('cpassword', '确认密码', 'trim|required|matches[password]');
            $this->form_validation->set_rules('email', '邮箱', 'required');

            if ($this->form_validation->run() == FALSE) {
                $this->layout->view('member/member_addV', $datas);
            } else {
                $datas             = $this->_post_data();
                $datas['reg_time'] = date('Y-m-d H:i:s');
                $datas['img']      = $this->_upload_img('img', 'local_first_add');

                if ($this->member->insert($datas)) {
                    $datas['error'] = "成功成为会员！";
                } else {
                    $datas['error'] = '新增失败！';
                }
                $this->layout->view('member/member_addV', $datas);
            }
        }

        /**
         * 前台会员注销
         */
        function logout()
        {
            $this->session->sess_destroy();
            redirect('index');
        }

        function _post_data()
        {
            $datas['name']  = $this->input->post('name'); //用户名
            $datas['phone'] = $this->input->post('phone'); //联系电话
            $datas['email'] = $this->input->post('email'); //邮箱
            $datas['url']   = $this->input->post('url'); //个人链接
            $datas['title'] = $this->input->post('title'); //头衔

            $datas['gender']     = $this->input->post('gender'); //性别
            $datas['birth_date'] = $this->input->post('birth_date'); //出生日期
            $datas['address']    = $this->input->post('address'); //地址

            if (strlen($this->input->post('password')) == 32) {
                $datas['password'] = $this->input->post('password'); //密码
            } else {
                $datas['password'] = md5($this->input->post('password')); //密码
            }

            if ($this->session->userdata('authority') != 1) {
                $datas['last_login_time'] = date('Y-m-d H:i:s'); //上次登入时间,即将这次的登入时间记作下次用的上次登入时间
                $datas['last_login_ip']   = $this->input->ip_address(); //上次登入ip,放在登录那一块儿做修改
            }

            return $datas;
        }

        function _upload_img($img_name, $img_url)
        {
            $config ['upload_path']   = './upload/avator/'; //设置头像上传目录
            $config ['allowed_types'] = 'gif|jpg|png';
            $config ['max_size']      = 1024 * 2;
            $config ['encrypt_name']  = TRUE;

            $this->load->library('upload', $config);

            if ($_FILES[$img_name]['size'] && $this->upload->do_upload($img_name)) {

                if ($img_url != 'local_first_add') { //如果不是第一次增加图片，即是更改图片
                    $url = './upload/avator/' . $img_url;
                    unlink($url); //删除原图
                }

                //取得上传成功数据
                $data = $this->upload->data();

                $img = $data['file_name'];
            } else {
                $img = $this->input->post($img_name); //图片
            }

            return $img;
        }
    }
